package com.ruijie.acl.web.config;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.AclInterfaceView;
import com.ruijie.acl.AclPlanInterface;
import com.ruijie.acl.service.AclInterfaceService;
import com.ruijie.acl.type.InterfaceAccessGroupTypeEnum;
import com.ruijie.acl.utils.AclUtil;
import com.ruijie.acl.utils.InterfaceSortUtil;
import com.ruijie.emp.device.Device;

/**
 * <p>
 * Title: ACL接口处理器
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-09-22
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * 
 * @author ASAI
 */
public class AclInterfaceProcessor {

	private AclInterfaceService aclInterfaceService;

	private Device device;

	private List<AclInterfaceView> aclInterfaceView;

	private String selectedInterfaceByAccessGroup;

	private Map<Long, List<AclPlanInterface>> interfaceMap = new HashMap<Long, List<AclPlanInterface>>();

	private List<AclPlanInterface> aclPlanInterfaceList = new ArrayList<AclPlanInterface>();

	/**
	 * 是否已经配置了接口
	 */
	private boolean configFlag = false;

	/**
	 * 客户端手动实例化Processor时的实例化构造函数
	 * 
	 * @param aclInterfaceService
	 */
	public AclInterfaceProcessor(AclInterfaceService aclInterfaceService) {
		this.aclInterfaceService = aclInterfaceService;
	}

	/**
	 * 取得设备的所有接口
	 * 
	 * @param device
	 *            设备
	 * @return 接口列表
	 */
	public List<AclInterfaceView> getAclInterfaceView(Device device) {
		if (aclInterfaceView == null || this.device == null) {
			this.device = device;
			aclInterfaceView = aclInterfaceService.getAclInterfaceViewByDevice(device);
		}
		return aclInterfaceView;
	}

	/**
	 * 取得设备的所有接口
	 * 
	 * @param device
	 *            设备
	 * @return 接口列表
	 */
	public List<AclInterfaceView> getAclInterfaceView() {
		if (this.device != null) {
			aclInterfaceView = aclInterfaceService.getAclInterfaceViewByDevice(this.device);
			aclInterfaceView = InterfaceSortUtil.sortInterfaceItemList(aclInterfaceView);
		}
		return aclInterfaceView;
	}

	/**
	 * 取得设备的所有接口
	 * 
	 * @param device
	 *            设备
	 * @return 接口列表
	 */
	public List<SelectItem> getAclInterface() {
		if (this.device != null) {
			this.aclInterfaceView = null;
			return InterfaceSortUtil.sortInterfaceItemListSelect(getAclInterfaceView(this.device));
		}
		return new ArrayList<SelectItem>();
	}

	public void save() {
		List<AclPlanInterface> aclPlanInterfaceList = convertString2AclPlanInterface(this.selectedInterfaceByAccessGroup);
		if (aclPlanInterfaceList != null) {
			this.interfaceMap.put(this.device.getId(), aclPlanInterfaceList);
		} else {
			this.interfaceMap.remove(this.device.getId());
		}
	}

	public void changeDevice(Device device) {
		this.device = device;
		if (this.interfaceMap.containsKey(device.getId())) {
			this.aclPlanInterfaceList = this.interfaceMap.get(device.getId());
			this.selectedInterfaceByAccessGroup = convertAclPlanInterface2String(this.aclPlanInterfaceList);
		} else {
			this.selectedInterfaceByAccessGroup = "";
		}
	}

	public String convertAclPlanInterface2String(Device device) {
		return convertAclPlanInterface2String(device.getId());
	}

	public String convertAclPlanInterface2String(Long id) {
		if (this.interfaceMap.containsKey(id)) {
			return convertAclPlanInterface2String(this.interfaceMap.get(id));
		}
		return "";
	}

	public String convertAclPlanInterface2String(List<AclPlanInterface> aclPlanInterfaceList) {
		String resultString = "";
		if (aclPlanInterfaceList != null && aclPlanInterfaceList.size() > 0) {
			for (AclPlanInterface aclPlanInterface : aclPlanInterfaceList) {
				resultString += aclPlanInterface.getDeviceInterface().getId() + ",";
				if (aclPlanInterface.getInAccessGroup() != null
						&& aclPlanInterface.getOutAccessGroup() != null) {
					resultString += "InOut," + aclPlanInterface.getInAccessGroup().getId() + ";";
				} else if (aclPlanInterface.getInAccessGroup() != null) {
					resultString += "In," + aclPlanInterface.getInAccessGroup().getId() + ";";
				} else if (aclPlanInterface.getOutAccessGroup() != null) {
					resultString += "Out," + aclPlanInterface.getOutAccessGroup().getId() + ";";
				}
			}
		}
		return resultString;
	}

	/**
	 * 把字符串转换成计划接口类型
	 * 
	 * @param planInterfaceString
	 * @return
	 */
	public List<AclPlanInterface> convertString2AclPlanInterface(String planInterfaceString) {
		List<AclPlanInterface> aclPlanInterfaceList = null;
		if (!AclUtil.empty(planInterfaceString)) {
			aclPlanInterfaceList = new ArrayList<AclPlanInterface>();
			planInterfaceString = planInterfaceString.trim();
			String[] arrAclPlanInterfaceList = planInterfaceString.split(";");
			for (String planInterfaceStr : arrAclPlanInterfaceList) {
				AclPlanInterface aclPlanInterface = new AclPlanInterface();
				String[] arrAclPlanInterface = planInterfaceStr.split(",");
				if (arrAclPlanInterface.length == 3) {
					aclPlanInterface.setDeviceInterface(aclInterfaceService.getDeviceInterface(Long
							.parseLong(arrAclPlanInterface[0])));
					try {
						AccessGroup accessGroup = aclInterfaceService.getAccessGroup(Long
								.parseLong(arrAclPlanInterface[2]));
						if (accessGroup != null) {
							String accessGroupType = arrAclPlanInterface[1];
							if (accessGroupType.equals(InterfaceAccessGroupTypeEnum.In.toString())) {
								aclPlanInterface.setInAccessGroup(accessGroup);
							} else if (accessGroupType.equals(InterfaceAccessGroupTypeEnum.Out
									.toString())) {
								aclPlanInterface.setOutAccessGroup(accessGroup);
							} else if (accessGroupType.equals(InterfaceAccessGroupTypeEnum.InOut
									.toString())) {
								aclPlanInterface.setInAccessGroup(accessGroup);
								aclPlanInterface.setOutAccessGroup(accessGroup);
							}
							aclPlanInterfaceList.add(aclPlanInterface);
						}
					} catch (Exception e) {
						//
					}

				}

			}
		}
		return aclPlanInterfaceList;
	}

	/**
	 * 对每个设备是否配置接口进行判断
	 * 
	 * @param id
	 *            设备ID号
	 * @return 是否已经配置接口，已经配置返回True。
	 */
	public boolean hasConfigInterface(Long id) {
		boolean result = false;
		if (this.getInterfaceMap() != null && this.getInterfaceMap().containsKey(id)) {
			List<AclPlanInterface> interfaceList = this.getInterfaceMap().get(id);
			if (interfaceList != null && interfaceList.size() > 0) {
				result = true;
			}
		}
		return result;
	}

	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}

	public String getSelectedInterfaceByAccessGroup() {
		return selectedInterfaceByAccessGroup;
	}

	public void setSelectedInterfaceByAccessGroup(String selectedInterfaceByAccessGroup) {
		this.selectedInterfaceByAccessGroup = selectedInterfaceByAccessGroup;
	}

	public Map<Long, List<AclPlanInterface>> getInterfaceMap() {
		return interfaceMap;
	}

	public void setInterfaceMap(Map<Long, List<AclPlanInterface>> interfaceMap) {
		this.interfaceMap = interfaceMap;
	}

	public List<AclPlanInterface> getAclPlanInterfaceList() {
		return aclPlanInterfaceList;
	}

	public void setAclPlanInterfaceList(List<AclPlanInterface> aclPlanInterfaceList) {
		this.aclPlanInterfaceList = aclPlanInterfaceList;
	}

	public boolean isConfigFlag() {
		configFlag = false;
		if (this.getInterfaceMap() != null && this.getInterfaceMap().size() > 0) {
			Collection<List<AclPlanInterface>> values = this.getInterfaceMap().values();
			Iterator<List<AclPlanInterface>> iterator = values.iterator();
			while (iterator.hasNext()) {
				List<AclPlanInterface> interfaceList = iterator.next();
				if (interfaceList != null && interfaceList.size() > 0) {
					configFlag = true;
					break;
				}
			}
		}
		return configFlag;
	}

}
